package com.gu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gu.VO.ServerResponse;
import com.gu.dao.LeavingMessageDAO;
import com.gu.dao.UserDAO;
import com.gu.entity.LeavingMessageDO;
import com.gu.entity.NoticeDO;
import com.gu.entity.UserDO;
import com.gu.resultMap.LeavingMessageWithAreaCode;
import com.gu.service.LeavingMessageService;
import com.gu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ProjectName: apartment-management-center
 * @Package: com.gu.service.impl
 * @ClassName: LeavingMessageServiceImpl
 * @Author: gusongliao
 * @Description: 留言功能实现曾
 * @createDate 2021/04/04 15:50
 */
@Service
public class LeavingMessageServiceImpl implements LeavingMessageService {

    @Autowired
    private LeavingMessageDAO leavingMessageDAO;

    @Autowired
    private UserDAO userDAO;

    @Override
    public PageInfo getLeavingMessageBySearchParam(Integer pageNum, Integer pageSize, String searchParam, String searchDate) {
        PageHelper.startPage(pageNum, pageSize);
        List<LeavingMessageDO> leavingMessageDOS = leavingMessageDAO.getRecordBySearchParam(searchParam, searchDate);
        PageInfo pageInfo = PageInfo.of(leavingMessageDOS);
        return pageInfo;
    }

    @Override
    public List<LeavingMessageWithAreaCode> getRecord(String searchParam, String searchDate) {
        List<LeavingMessageDO> leavingMessageDOS = leavingMessageDAO.getRecordBySearchParam(searchParam, searchDate);
        List<LeavingMessageWithAreaCode> leavingMessageWithAreaCodes = new ArrayList<>();
        List<UserDO> userDOList = userDAO.getAllUser();
        if (leavingMessageDOS != null && userDOList != null) {
            for (LeavingMessageDO leavingMessageDO : leavingMessageDOS) {
                LeavingMessageWithAreaCode leavingMessageWithAreaCode = new LeavingMessageWithAreaCode();
                BeanUtils.copyProperties(leavingMessageDO, leavingMessageWithAreaCode);
                for (UserDO userDO : userDOList) {
                    if (leavingMessageWithAreaCode.getParentId().equals(userDO.getId())) {
                        leavingMessageWithAreaCode.setParentName(userDO.getUserNickname());
                    }
                    if (leavingMessageWithAreaCode.getUserId().equals(userDO.getId())) {
                        leavingMessageWithAreaCode.setUserName(userDO.getUserNickname());
                        leavingMessageWithAreaCode.setImgInfo(userDO.getImgPath());
                    }
                }
                if (leavingMessageWithAreaCode.getParentName() == null) {
                    leavingMessageWithAreaCode.setLeavingInfo(leavingMessageWithAreaCode.getUserName());
                } else {
                    leavingMessageWithAreaCode.setLeavingInfo(leavingMessageWithAreaCode.getUserName() + "回复：" + leavingMessageWithAreaCode.getParentName());
                }
                leavingMessageWithAreaCodes.add(leavingMessageWithAreaCode);
            }
        }
        return leavingMessageWithAreaCodes;
    }

    @Override
    public ServerResponse insertLeavingMessage(LeavingMessageDO leavingMessageDO) {
        if (leavingMessageDO.getAreaCode() == null) {
            leavingMessageDO.setAreaCode(UUID.randomUUID().toString());
        }
        leavingMessageDO.setCreateTime(new Date());
        leavingMessageDO.setUpdateTime(new Date());
        int result = leavingMessageDAO.insert(leavingMessageDO);
        if (result == 1) {
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(7, "新增公告失败");
    }

    @Override
    public LeavingMessageDO getById(Integer id) {
        return leavingMessageDAO.getById(id);
    }

    @Override
    public ServerResponse deleteByIds(Integer[] ids) {
        Integer result = leavingMessageDAO.deleteByIds(ids);
        if (result == ids.length) {
            return ServerResponse.SUCCESS;
        }
        return ServerResponse.error(14, "删除公告失败");
    }

    @Override
    public ServerResponse getRecordByAreaCode(String areaCode) {

        List<LeavingMessageDO> leavingMessageDOS = leavingMessageDAO.getRecordByAreaCode(areaCode);
        List<LeavingMessageWithAreaCode> leavingMessageWithAreaCodes = new ArrayList<>();
        List<UserDO> userDOList = userDAO.getAllUser();
        if (leavingMessageDOS != null && userDOList != null) {
            leavingMessageDOS.forEach(e -> {
                LeavingMessageWithAreaCode leavingMessageWithAreaCode = new LeavingMessageWithAreaCode();
                BeanUtils.copyProperties(e, leavingMessageWithAreaCode);
                for (UserDO userDO : userDOList) {
                    if (leavingMessageWithAreaCode.getParentId().equals(userDO.getId())) {
                        leavingMessageWithAreaCode.setParentName(userDO.getUserNickname());
                    }
                    if (leavingMessageWithAreaCode.getUserId().equals(userDO.getId())) {
                        leavingMessageWithAreaCode.setUserName(userDO.getUserNickname());
                        leavingMessageWithAreaCode.setImgInfo(userDO.getImgPath());
                    }
                }
                if (leavingMessageWithAreaCode.getParentName() == null) {
                    leavingMessageWithAreaCode.setLeavingInfo(leavingMessageWithAreaCode.getUserName());
                } else {
                    leavingMessageWithAreaCode.setLeavingInfo(leavingMessageWithAreaCode.getUserName() + "回复：" + leavingMessageWithAreaCode.getParentName());
                }
                leavingMessageWithAreaCodes.add(leavingMessageWithAreaCode);
            });
            return ServerResponse.success(leavingMessageWithAreaCodes);
        }
        return ServerResponse.error(18, "查询留言失败");
    }

    @Override
    public ServerResponse deleteLeavingMsg(LeavingMessageDO leavingMessageDO) {
        leavingMessageDAO.deleteLeavingMsg(leavingMessageDO);
        return ServerResponse.SUCCESS;
    }

}
